package handler

import (
	__ "api/proto"
	"api/request"
	"github.com/gin-gonic/gin"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"log"
	"net/http"
)

func AddVideo(c *gin.Context) {
	var req request.AddVideo
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	conn, err := grpc.NewClient("127.0.0.1:8888", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewUserClient(conn)
	add, err := c1.AddVideo(c, &__.AddVideoRequest{
		UserId: int64(req.UserID),
		Name:   req.Name,
		Like:   int64(req.Like),
		People: int64(req.People),
		Gift:   int64(req.Gift),
		Img:    req.Img,
	})
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"data": add,
		"msg":  "添加视频成功",
	})
}

func ListVideo(c *gin.Context) {

}
func InfoVideo(c *gin.Context) {
	var req request.Info
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	conn, err := grpc.NewClient("127.0.0.1:8888", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c1 := __.NewUserClient(conn)
	info, err := c1.InfoVideo(c, &__.InfoVideoRequest{
		Id: int64(req.Id),
	})
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"data": info,
		"msg":  "列表展示成功",
	})
}
